/**
 * 
 */
package cn.salesuite.movie.activity;

import java.io.Serializable;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.salesuite.movie.BaseActivity;
import cn.salesuite.movie.config.Constant;
import cn.salesuite.movie.db.DatabaseHelper;
import cn.salesuite.movie.domain.Cinema;

/**
 * @author Tony Shen
 *
 */
public class OrderActivity extends BaseActivity{

	private Button backBtn,nextBtn;
	private RelativeLayout moiveBtn,payBtn;
	private TextView priceTxt;
	
	private DatabaseHelper m_databaseHelper;
	private String cityCode,couponType;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order);

		m_databaseHelper = DatabaseHelper.GetInstance(this);
		
		findViews();
		initViews();
		
		addFootMenuView();
		menuSelceted(2);
	}

	private void findViews() {
		backBtn = (Button) findViewById(R.id.btn_back);
		nextBtn = (Button) findViewById(R.id.btn_next);
		moiveBtn = (RelativeLayout) findViewById(R.id.moiveBtn);
		payBtn = (RelativeLayout) findViewById(R.id.payBtn);
		priceTxt = (TextView) findViewById(R.id.price_value);
	}

	private void initViews() {
		cityCode = (String) app.session.get(Constant.CITY_CODE);
		
		nextBtn.setVisibility(View.GONE);
		
		backBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				finish();
			}
			
		});
		
		if (getIntent()!=null) {
			couponType = getIntent().getStringExtra("couponType");
		}

		getCouponPrice(couponType);
		
		moiveBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				mProgressDialog = getProgressDialog(OrderActivity.this);
				
				new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							List<Cinema> cinemaList = m_databaseHelper.getCinemaListByCoupon(cityCode,couponType);
							Intent i = new Intent(OrderActivity.this,CinemaListForReleaseActivity.class);
							i.putExtra("cinemaList", (Serializable)cinemaList);
							startActivity(i);
						} finally {
							closeProgressDialog(mProgressDialog);
						}
					}
					
				}).start();

			}
			
		});
		
		payBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent i = new Intent(OrderActivity.this,PayActivity.class);
				i.putExtra("couponType", couponType);
				startActivity(i);
			}
			
		});
	}
	
	private void getCouponPrice(String couponType) {
		if (couponType == null) {
			priceTxt.setText(" ");
		} else if ("F".equals(couponType)) {
			priceTxt.setText((String)app.session.get(Constant.F_COUPON_KEY)+"Ԫ");
		} else if ("G".equals(couponType)) {
			priceTxt.setText((String)app.session.get(Constant.G_COUPON_KEY)+"Ԫ");
		} else if ("H".equals(couponType)) {
			priceTxt.setText((String)app.session.get(Constant.H_COUPON_KEY)+"Ԫ");
		} else if ("I".equals(couponType)) {
			priceTxt.setText((String)app.session.get(Constant.I_COUPON_KEY)+"Ԫ");
		} else if ("J".equals(couponType)) {
			priceTxt.setText((String)app.session.get(Constant.J_COUPON_KEY)+"Ԫ");
		}
	}
}
